<template>
  <div class="app-container">
    <!--工具栏-->
    <el-card>
      <div class="head-container">
        <el-input
          v-model="query.blurry"
          clearable
          size="small"
          placeholder="请输入客户端名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="toQuery"
        />
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetQuery">重置</el-button>
        <div class="crud-opts">
          <span class="crud-opts-left">
            <!--左侧插槽-->
            <slot name="left" />
            <el-button
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-plus"
              @click="toAdd"
            >
              新增
            </el-button>
            <el-button
              slot="reference"
              class="filter-item"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="toDelete"
            >
              删除
            </el-button>
          </span>
        </div>
      </div>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="loading"
        :data="dataList"
        highlight-current-row
        style="width: 100%;"
        @selection-change="selectionChangeHandler"
        @current-change="handleCurrentChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column :show-overflow-tooltip="true" prop="clientName" label="客户端名称" />
        <el-table-column :show-overflow-tooltip="true" prop="clientKey" label="客户端key" />
        <el-table-column :show-overflow-tooltip="true" prop="clientSecret" label="客户端密钥" />
        <el-table-column :show-overflow-tooltip="true" prop="zt" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.zt">启用</span>
            <span v-else>禁用</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="toEdit(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" size="mini" @click="toDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="query.page"
        :page-size="query.size"
        :total="pagerSize"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @current-change="pagerHandleCurrentChange"
        @size-change="pagerHandleSizeChange"
      />
    </el-card>
    <entity ref="entity" @fetch-data="callBack" />
  </div>
</template>
<script>
import { queryAllNoticeTemplate, deleteTemplate } from '@/api/webhook/config/index';
import Entity from './entity/index';
export default {
  name: 'WebHookManagerIndex',
  components: { Entity },
  data() {
    return {
      pagerSize: 0,
      query: { page: 1, size: 10, blurry: '', createTime: null },
      loading: false,
      dataList: [],
      selectId: [],
      labels: [],
      entity: Object.assign({})
    };
  },
  created() {
    this.initData();
  },
  methods: {
    toQuery() {
      this.initData();
    },
    toAdd() {
      this.$refs.entity.openEntity();
    },
    toDelete(data) {
      if (data.id !== null && undefined !== data.id) { // 单个删除
        this.selectId = [];
        this.selectId.push(data.id);
        this.$confirm('此操作将永久删除' + data.title + ', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteTemplate(this.selectId).then(res => {
            this.$message.success('删除成功！');
            this.initChild();
            this.selectId = [];
          }).catch(() => {
            this.$message.error('删除失败！');
          });
        }).catch(() => {
          this.$message.success('取消删除成功！');
        });
      } else {
        if (this.selectId === null || this.selectId.length === 0) {
          this.$message.info('请选择删除项！');
        } else {
          this.$confirm('此操作将永久删除选中记录，是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteTemplate(this.selectId).then(res => {
              this.$message.success('删除成功！');
              this.initChild();
              this.selectId = [];
            }).catch(() => {
              this.$message.error('删除失败！');
            });
          }).catch(() => {
            this.$message.success('取消删除成功！');
          });
        }
      }
    },
    toEdit(data) {
      this.$refs.entity.openEntity(data);
    },
    initData() {
      this.loading = true;
      queryAllNoticeTemplate(this.query).then(res => {
        this.loading = false;
        const { data } = res;
        this.pagerSize = data.pagerSize;
        if (data !== null) {
          this.dataList = data.data;
        }
      }).catch(() => {
        this.loading = false;
      });
    },
    resetQuery() {
      Object.assign(this.$data.query, this.$options.data.call(this).query);
      this.toQuery();
    },
    selectionChangeHandler(val) {
      if (val === null || val.length === 0) {
        this.selectId = [];
      } else {
        this.selectId = val.map((res) => {
          return res.id;
        });
      }
    },
    handleCurrentChange(val) {
      if (undefined !== val && val !== null) {
        this.entity = val;
        this.selectId = [];
        this.selectId.push(val.id);
      } else {
        this.entity = Object.assign({});
        this.selectId = [];
      }
    },
    pagerHandleCurrentChange(val) {
      this.query.page = val;
      this.initData();
    },
    pagerHandleSizeChange(val) {
      this.query.page = 1;
      this.query.size = val;
      this.initData();
    },
    callBack() {
      this.initData();
    }
  }
};
</script>

<style scoped>
.custom-span {
  border-radius: 2px;
  color: #fff;
  padding: 2px 5px;
  box-sizing: border-box;
}
</style>
